package com.xie.o2o.service.impl;

import com.xie.o2o.dao.ShopDao;
import com.xie.o2o.dto.ShopExecution;
import com.xie.o2o.entity.Shop;
import com.xie.o2o.enums.ShopStateEnum;
import com.xie.o2o.service.ShopService;
import com.xie.o2o.util.ImageUtil;
import com.xie.o2o.util.PageCalculator;
import com.xie.o2o.util.PathUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @create: xie
 */
@Service
public class ShopServiceImpl implements ShopService {
    private final static Logger log = LoggerFactory.getLogger(ShopServiceImpl.class);
    @Autowired
    private ShopDao shopDao;

    /**
     * 获取店铺列表分页显示
     *
     * @param shopCondition
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
        //将前台页码转换成行码
        int i = PageCalculator.calculateRowIndex(pageIndex, pageSize);
        //根据条件查询店铺
        List<Shop> shopList = shopDao.queryShopList(shopCondition, i, pageSize);
        //根据相同条件查询总数量
        int count = shopDao.queryShopCount(shopCondition);
        ShopExecution shopExecution = new ShopExecution();
        if (shopList != null) {
            shopExecution.setShopList(shopList);
            shopExecution.setCount(count);
        } else {
            shopExecution.setState(ShopStateEnum.INNER_ERROR.getState());
        }
        return shopExecution;
    }

    /**
     * 添加店铺
     *
     * @param shop
     * @param shopImg
     * @return
     */
    @Override
    @Transactional
    public ShopExecution addShop(Shop shop, MultipartFile shopImg) throws RuntimeException {
        if (shop == null) {
            return new ShopExecution(ShopStateEnum.NULL_SHOP);
        }
        //设置审核
        shop.setEnableStatus(0);
        shop.setCreateTime(new Date());
        shop.setLastEditTime(new Date());
        shopDao.insertShop(shop);
        //添加缩略图
        addShopImg(shop, shopImg);
        shopDao.updateShop(shop);
        return new ShopExecution(ShopStateEnum.CHECK, shop);
    }

    //添加缩略图
    private void addShopImg(Shop shop, MultipartFile shopImg) {
        //店家各自的存储路径
        String shopImgPath = PathUtil.getShopImgPath(shop.getShopId());
        //存储缩略图，返回数据库需要的信息
        String s = ImageUtil.generateThumbnail(shopImg, shopImgPath);
        shop.setShopImg(s);
    }

    @Override
    public Shop getByShopId(Long shopId) {
        return shopDao.queryShop(shopId);
    }

    /**
     * 修改店铺信息
     *
     * @param shop
     * @param shopImg
     * @return
     * @throws RuntimeException
     */
    @Override
    public ShopExecution modifyShop(Shop shop, MultipartFile shopImg) throws RuntimeException {
        if (shop == null || shop.getShopId() == null) {
            return new ShopExecution(ShopStateEnum.NULL_SHOP);
        } else {
            //是否处理图片
            try {
                if (shopImg != null) {
                    //不为空表示有新图片，删除旧图片
                    Shop shop1 = shopDao.queryShop(shop.getShopId());
                    if (shop1.getShopImg() != null) {
                        ImageUtil.deleteFileOrPath(shop1.getShopImg());
                    }
                    //添加新图片
                    addShopImg(shop, shopImg);
                }
                //更改店铺信息
                shop.setLastEditTime(new Date());
                int i = shopDao.updateShop(shop);
                if (i <= 0) {
                    return new ShopExecution(ShopStateEnum.INNER_ERROR);
                } else {
                    shop = shopDao.queryShop(shop.getShopId());
                    return new ShopExecution(ShopStateEnum.SUCCESS, shop);
                }
            } catch (Exception e) {
                throw new RuntimeException("修改店铺信息失败:" + e.getMessage());
            }
        }
    }

}
